﻿/*
    Copyright (c) 2013-2014 Contributors as noted in the AUTHORS file

    This file is part of  RuNet Importer for NodeXL.

    RuNet Importer is free software; you can redistribute it and/or modify it under
    the terms of the Microsoft Public License (Ms-PL), or (at your option) any later 
	version.

    RuNet Importer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
	See the Microsoft Public License for more details.

    You should have received a copy of the Microsoft Public License
    along with this program.  If not, see <http://www.microsoft.com/en-us/openness/licenses.aspx>.
*/

using System;
using System.Diagnostics;
using System.Windows.Forms;
using rcsir.net.ok.importer.Events;
using rcsir.net.ok.importer.Storages;

namespace rcsir.net.ok.importer.Dialogs
{
    public partial class OKLoginDialog : Form
    {
        private string authUri;

        public string AuthUri { set { authUri = value; } }

        public event EventHandler<CommandEventArgs> CommandEventHandler;

        public OKLoginDialog()
        {
            InitializeComponent();
        }

        public void Login()
        {
            Debug.WriteLine("Navigate");
            webBrowserLogin.Navigate(authUri);
            ShowDialog();
        }

        public void Logout()
        {
            webBrowserLogin.Navigate(RequestParametersStorage.StartUrl);
            ShowDialog();
        }

        private void webBrowserLogin_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Debug.WriteLine("DocumentCompleted");
            string stringUrl = webBrowserLogin.Url.ToString();
            Debug.WriteLine(stringUrl);
            var evnt = new CommandEventArgs(CommandEventArgs.Commands.GetAccessToken, stringUrl);
            DispatchEvent(evnt);
//          DisableComponents(fcbDialog);
        }

        protected virtual void DispatchEvent(CommandEventArgs e)
        {
            EventHandler<CommandEventArgs> handler = CommandEventHandler;
            if (handler != null)
                handler(this, e);
        }
    }
}
